<?php
	if ((isset($_GET['tag']) && $_GET['tag'] != '') && (isset($_GET['id']) && $_GET['id'] != '')) {
		$tag = $_GET['tag'];
		$id = $_GET['id'];
		include_once 'DB_Functions.php';
		$response = array();
		$r = array("tag" => $tag, "success" => 0, "matches" => "", "error" => 0);
		$matches = "matches: [";
		$returnarray = array();
		$dbf = new DB_Functions();
		if ($tag == 'getMatches') {
			$result = $dbf->getMatch();
				if ($result->fetch_assoc() != false) {
					$r['success'] = 1;
					
					while ($row = $result->fetch_assoc()) {
						$response['match']['match_id'] = $row['match_id'];
						$response['match']['home'] = $row['home'];
						$response['match']['home_name'] = $row['home_name'];
						$response['match']['away'] = $row['away'];
						$response['match']['away_name'] = $row['away_name'];
						$response['match']['matchdate'] = $row['matchdate'];
						$response['match']['info'] = $row['info'];
						$response['match']['running'] = $row['running'];
						$response['match']['overtime_length'] = $row['overtime_length'];
						$response['match']['head1'] = $row['head1'];
						$response['match']['head2'] = $row['head2'];
						$response['match']['linesman1'] = $row['linesman1'];
						$response['match']['linesman2'] = $row['linesman2'];
						$response['match']['spectators'] = $row['spectators'];
						$response['match']['roundid'] = $row['roundid'];
						$response['match']['active'] = $row['active'];
						array_push($returnarray,$response);
					}
					$r['matches']=$returnarray;
					echo json_encode($r);
				}
			else {
				$response["error"] = 1;
				$response["error_msg"] = "Fehler ist aufgetreten";
				echo json_encode($response);
			}
		$dbf->closeResult();
		}
		else if($tag == 'getTickerEntries') {
			$result = $dbf->getEntries($id);
			if ($result->fetch_assoc() != false) {
				$r['success'] = 1;
				$response['matches'];
				array_push($returnarray, $r);
				while ($row = $result->fetch_assoc()) {
					$response['entry']['entry_id'] = $row['entry_id'];
					$response['entry']['match_id'] = $row['match_id'];
					$response['entry']['entry_type'] = $row['entry_type'];
					$response['entry']['team'] = $row['team'];
					$response['entry']['playtime'] = $row['playtime'];
					$response['entry']['player1'] = $row['player1'];
					$response['entry']['player2'] = $row['player2'];
					$response['entry']['player3'] = $row['player3'];
					$response['entry']['powerplay'] = $row['powerplay'];
					$response['entry']['penalty_time'] = $row['penalty_time'];
					$response['entry']['penalty_reason'] = $row['penalty_reason'];
					$response['entry']['penalty_extension'] = $row['penalty_extension'];
					$response['entry']['info'] = $row['info'];
					$response['entry']['entry_time'] = $row['entry_time'];
					array_push($returnarray,$response);
				}

				$r['matches']=$returnarray;
				echo json_encode($r);
			}
			else {
				$response["error"] = 1;
				$response["error_msg"] = "Fehler ist aufgetreten";
				echo json_encode($response);
			}
		}
	}
?>